package com.example.ywj.toutiao.utils;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.ywj.toutiao.R;
import com.example.ywj.toutiao.bean.News;

import java.util.ArrayList;
import java.util.List;

public class MyNewAdapter extends BaseAdapter {
    public List<News> list;
    public MyNewAdapter(List<News> list){
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        String arr[] = list.get(position).getImages().split("&");
        if(arr.length ==1){
            return 0;
        }else if(arr.length==2){
            return 1;
        }else if(arr.length >= 3){
            return 2;
        }else{
            return 100;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int currentType = getItemViewType(position);
        if(currentType ==0){
            Item1ViewHolder item1ViewHolder ;
            if(convertView == null) {
                //获取布局管理器
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                convertView = inflater.inflate(R.layout.mynews_item1, null);
                item1ViewHolder = new Item1ViewHolder();
                item1ViewHolder.item1_avatar = convertView.findViewById(R.id.item1_avatar);
                item1ViewHolder.item1_content = convertView.findViewById(R.id.item1_content_text);
                item1ViewHolder.item1_content_pic = convertView.findViewById(R.id.item1_content_pic);
                item1ViewHolder.item1_nickname = convertView.findViewById(R.id.item1_nickname);
                item1ViewHolder.item1_time = convertView.findViewById(R.id.item1_time);
                convertView.setTag(item1ViewHolder);
            }else{
                item1ViewHolder = (Item1ViewHolder)convertView.getTag();
            }
            item1ViewHolder.item1_time.setText(list.get(position).getTime());
            item1ViewHolder.item1_nickname.setText(list.get(position).getAuthor());
            item1ViewHolder.item1_content.setText(list.get(position).getContent());
            RequestOptions requestOptions = new RequestOptions();
            requestOptions.placeholder(R.drawable.placeholder).error(R.drawable.error_img);
            String url = Constant.BASEURL+"public/images/"+list.get(position).getAvatar()+".jpg";
            String url1 = Constant.BASEURL+"public/images/"+list.get(position).getImages()+".jpg";
            Glide.with(parent.getContext()).load(url).apply(requestOptions).into(item1ViewHolder.item1_avatar);
            Glide.with(parent.getContext()).load(url1).apply(requestOptions).into(item1ViewHolder.item1_content_pic);
        }else if(currentType == 1){
            Item2ViewHolder item2ViewHolder ;
            if(convertView == null) {
                //获取布局管理器
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                convertView = inflater.inflate(R.layout.mynews_item2, null);
                item2ViewHolder = new Item2ViewHolder();
                item2ViewHolder.item2_avatar = convertView.findViewById(R.id.item2_avatar);
                item2ViewHolder.item2_content = convertView.findViewById(R.id.item2_content_text);
                item2ViewHolder.item2_content_pic1 = convertView.findViewById(R.id.item2_content_pic1);
                item2ViewHolder.item2_content_pic2 = convertView.findViewById(R.id.item2_content_pic2);
                item2ViewHolder.item2_nickname = convertView.findViewById(R.id.item2_nickname);
                item2ViewHolder.item2_time = convertView.findViewById(R.id.item2_time);
                convertView.setTag(item2ViewHolder);
            }else{
                item2ViewHolder = (Item2ViewHolder)convertView.getTag();
            }
            item2ViewHolder.item2_time.setText(list.get(position).getTime());
            item2ViewHolder.item2_nickname.setText(list.get(position).getAuthor());
            item2ViewHolder.item2_content.setText(list.get(position).getContent());
            RequestOptions requestOptions = new RequestOptions();
            requestOptions.placeholder(R.drawable.placeholder).error(R.drawable.error_img);
            String url = Constant.BASEURL+"public/images/"+list.get(position).getAvatar()+".jpg";
            String arr[] = list.get(position).getImages().split("&");
            String url1 = Constant.BASEURL+"public/images/"+arr[0]+".jpg";
            String url2 = Constant.BASEURL+"public/images/"+arr[1]+".jpg";
            Glide.with(parent.getContext()).load(url).apply(requestOptions).into(item2ViewHolder.item2_avatar);
            Glide.with(parent.getContext()).load(url1).apply(requestOptions).into(item2ViewHolder.item2_content_pic1);
            Glide.with(parent.getContext()).load(url2).apply(requestOptions).into(item2ViewHolder.item2_content_pic2);
        }else if(currentType == 2){
            Item3ViewHolder item3ViewHolder ;
            if(convertView == null) {
                //获取布局管理器
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                convertView = inflater.inflate(R.layout.mynews_item3, null);
                item3ViewHolder = new Item3ViewHolder();
                item3ViewHolder.item3_avatar = convertView.findViewById(R.id.item3_avatar);
                item3ViewHolder.item3_content = convertView.findViewById(R.id.item3_content_text);
                item3ViewHolder.item3_nickname = convertView.findViewById(R.id.item3_nickname);
                item3ViewHolder.item3_time = convertView.findViewById(R.id.item3_time);
                item3ViewHolder.item3_gridView = convertView.findViewById(R.id.item3_gridView);
                convertView.setTag(item3ViewHolder);
            }else{
                item3ViewHolder = (Item3ViewHolder) convertView.getTag();
            }
            item3ViewHolder.item3_time.setText(list.get(position).getTime());
            item3ViewHolder.item3_nickname.setText(list.get(position).getAuthor());
            item3ViewHolder.item3_content.setText(list.get(position).getContent());
            RequestOptions requestOptions = new RequestOptions();
            requestOptions.placeholder(R.drawable.placeholder).error(R.drawable.error_img);
            String url = Constant.BASEURL+"public/images/"+list.get(position).getAvatar()+".jpg";
            Glide.with(parent.getContext()).load(url).apply(requestOptions).into(item3ViewHolder.item3_avatar);
            String arr[] = list.get(position).getImages().split("&");
            List<String> list = new ArrayList<>();
            for(int i = 0;i<arr.length;i++){
                list.add(arr[i]);
            }
            PicAdapter picAdapter = new PicAdapter(list);
            item3ViewHolder.item3_gridView.setAdapter(picAdapter);
            item3ViewHolder.item3_gridView.setClickable(false);
            item3ViewHolder.item3_gridView.setPressed(false);
            item3ViewHolder.item3_gridView.setEnabled(false);
        }
        return convertView;
    }
    public class Item1ViewHolder{
        ImageView item1_avatar;
        ImageView item1_content_pic;
        TextView item1_nickname;
        TextView item1_time;
        TextView item1_content;
    }
    public class Item2ViewHolder{
        ImageView item2_avatar;
        ImageView item2_content_pic1;
        ImageView item2_content_pic2;
        TextView item2_nickname;
        TextView item2_time;
        TextView item2_content;
    }
    public class Item3ViewHolder{
        ImageView item3_avatar;
        GridView item3_gridView;
        TextView item3_nickname;
        TextView item3_time;
        TextView item3_content;
    }
}
